package com.huang.single;

import java.lang.reflect.Constructor;

//懒汉式单例
public class LazyMan {
    private static boolean HJW=false;

    private LazyMan(){
//            if(HJW==false){
//                HJW=true;
//            }else{
//                throw new RuntimeException("不要用反射破坏异常，，，，");
//
//            }

        if(lazyMan!=null){
            throw new RuntimeException("不要用反射破坏异常，，，，");
        }



    }

    private static LazyMan lazyMan;

    public static LazyMan getInstance(){
        if(lazyMan==null){   //双检索
            synchronized (LazyMan.class){
                if(lazyMan==null){
                    lazyMan=new LazyMan();
                }
            }
        }

        return lazyMan;
    }

    public static void main(String[] args) throws Exception {

     //   LazyMan lazyMan1 = LazyMan.getInstance();
        Constructor<LazyMan> constructor = LazyMan.class.getDeclaredConstructor(null);
        constructor.setAccessible(true);  //无视私有权限
        LazyMan lazyMan = constructor.newInstance();
        LazyMan lazyMan2 = constructor.newInstance();
        System.out.println(lazyMan);
        System.out.println(lazyMan2);
    }
}
